﻿Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCOM()
    End Sub

    Private Sub LoadCOM()
        For Each s As String In System.IO.Ports.SerialPort.GetPortNames()
            Me.cboCOM.Items.Add(s.Replace("COM", ""))
        Next
        If cboCOM.Items.Count > 0 Then
            cboCOM.SelectedIndex = 0
        End If
    End Sub

    Private Sub btnDia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDia.Click
        Dim phone As String = Me.txtPhone.Text.Trim

        If phone = "" Then
            Me.txtPhone.Focus()
            Exit Sub
        End If

        Dim result As Boolean

        Me.Cursor = Cursors.WaitCursor
        result = COMHelper.Instance.InitModem(Me.cboCOM.Text, phone)
        Me.Cursor = Cursors.Default

        If result = False Then
            msg.ShowWarning("拨号失败。")
        Else
            msg.Show("拨号成功。")
            Me.Panel1.Enabled = True
        End If

    End Sub
 
    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

        'Dim bValue As Byte = Me.nudData.Value - 1
        'Dim shortValue As Short = Me.nudData.Value
        'Dim sngValue As Single = Me.nudData.Value

        If Me.optByte.Checked Then
            Dim b() As Byte
            b = COMHelper.Instance.Send4Byte(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If b Is Nothing Then
                'msgbox ""
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To b.Length - 1
                    'str &= String.Format("{0:X2},", b(i))
                    str &= String.Format("{0},", b(i))
                Next

                addmsg(str)

                'MsgBox(System.Text.Encoding.Default.GetString(b))
            End If


        ElseIf Me.optShort.Checked Then
            Dim s() As Short
            s = COMHelper.Instance.Send4Short(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If s Is Nothing Then
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To s.Length - 1
                    str &= String.Format("{0},", s(i))
                Next
                addmsg(str)
            End If

        ElseIf Me.optSingle.Checked Then
            Dim ss() As Single
            ss = COMHelper.Instance.Send4Single(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), Convert.ToUInt16(Me.nudData.Value))

            If ss Is Nothing Then
                addmsg("超时!")
            Else
                Dim str As String = ""
                For i As Integer = 0 To ss.Length - 1
                    str &= String.Format("{0},", ss(i).ToString())
                Next
                addmsg(str)
            End If

        End If
    End Sub

    Dim _i As Integer = 0

    Private Sub addmsg(ByVal str As String)
        _i += 1

        'Debug.WriteLine(Now.ToString("HH:mm:ss.") & Now.Millisecond & " #" & _i & "  ")
        'Exit Sub


        If Me.txtLog.Text.Trim <> "0" Then
            Me.txtLog.Text &= str & ControlChars.NewLine
        Else
            Me.txtLog.Text = ControlChars.NewLine & Now.ToString("HH:mm:ss.") & Now.Millisecond & " #" & _i & "  " & str & ControlChars.NewLine & ControlChars.NewLine & Me.txtLog.Text
        End If

    End Sub

    Private Sub ShowData(Of T)(ByVal v() As T)
        For Each tv As T In v
            Me.txtLog.Text &= tv.ToString() & ControlChars.NewLine
        Next
    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim result As Boolean

        If Me.optByte.Checked Then
            Dim bValue As Byte = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), bValue)
        ElseIf Me.optShort.Checked Then
            Dim shortValue As Short = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), shortValue)
        ElseIf Me.optSingle.Checked Then
            Dim sngValue As Single = Me.nudData.Value
            result = COMHelper.Instance.Write(Convert.ToByte(Me.nudAddress.Value), Convert.ToByte(Me.nudFunction.Value), Convert.ToUInt16(Me.nudOffset.Value), sngValue)
        End If


        If result Then
            msg.Show("写成功")
        Else
            msg.Show("写失败")
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not SerialPort1.IsOpen Then
            SerialPort1.Open()
        End If
        Dim cmd As String

        cmd = Me.txtPhone.Text.Trim
        cmd &= ControlChars.Cr
        Me.SerialPort1.Write(cmd)

        Exit Sub
        cmd = "AT"
        cmd &= ControlChars.Cr
        Me.SerialPort1.Write(cmd)

        MsgBox(Now)

        cmd = "atdt222"
        cmd &= ControlChars.Cr
        Me.SerialPort1.Write(cmd)
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Debug.WriteLine(Me.SerialPort1.ReadExisting())
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not SerialPort1.IsOpen Then
            SerialPort1.Open()
        End If
        Dim cmd As String

        cmd = Me.txtPhone.Text.Trim

        Me.SerialPort1.Write(cmd)
    End Sub

    Private Sub SerialPort1_PinChanged(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialPinChangedEventArgs) Handles SerialPort1.PinChanged
        MsgBox("PinChanged")
    End Sub

    Private Sub SerialPort1_ErrorReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialErrorReceivedEventArgs) Handles SerialPort1.ErrorReceived
        MsgBox("ERROR")
    End Sub

    Private Sub btnDirectConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDirectConnect.Click
        COMHelper.Instance.InitSerialPort(1, IO.Ports.Parity.Even)

        Me.Panel1.Enabled = True

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i As Integer = 0 To Me.txtPhone.Text
            Me.btnRead.PerformClick()
        Next
        
        'Me.Timer1.Enabled = Not Me.Timer1.Enabled
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtLog.Text = ""
    End Sub

    Private Sub nudFunction_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudFunction.ValueChanged
        Select Case nudFunction.Value
            Case &H70, &H71
                optByte.Checked = True

            Case &H72, &H73
                optShort.Checked = True

            Case &H74, &H75
                optSingle.Checked = True

        End Select
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        For i As Integer = 0 To 0
            Me.btnRead.PerformClick()
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim b() As Byte

        For j As Integer = 0 To Me.nudData.Value
            b = COMHelper.Instance.Send4Byte(3, 1, 2, 1)

            If b Is Nothing Then
                'msgbox ""
                addmsg("超时!")
            Else

                Dim str As String = ""
                For i As Integer = 0 To b.Length - 1
                    'str &= String.Format("{0:X2},", b(i))
                    str &= String.Format("{0},", b(i))
                Next

                addmsg(str)

                'MsgBox(System.Text.Encoding.Default.GetString(b))
            End If
        Next
    

    End Sub

    Private Sub btnTimeout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimeout.Click
        COMHelper.Instance.timeout = Me.nudTimeout.Value
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        COMHelper.Instance.Close()
    End Sub
End Class